package portal.conf;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.common.MyStringUtils;
import portal.conf.entity.BaseConf;
import portal.conf.entity.HostEntity;
@Component
@Scope("singleton")
public class HostsConf extends AbstractConf{

	protected  String startTitle="Begin Host";
	protected  String endTitle="End Host";
	//protected  String fileName="//app//unischeduler//conf//hosts.conf";
	public HostsConf(){
		if(IsDebug)
			fileName="//app//unischeduler//conf//hosts.conf";
	}
	@Value("${conf.hostfile}")  
	public void setFileName(String fname) {  
		fileName = fname;  
	} 
	@Value("${conf.hostfile.end}")  
	public void setEndTitle(String fname) {  
		endTitle = fname;  
	}
	@Value("${conf.hostfile.start}")   
	public void setStartTitle(String fname) {  
		startTitle = fname;  
	}

	@Override
	protected boolean isConfStart(String line) {
		if(line.contains(startTitle))
			return true;
		return false;
	}
	@Override
	protected  BaseConf analysiConf(String tempString,int line,Class<?> t,BaseConf qe){
		
		try {
			//if()
			if(qe!=null)
				return qe;
			qe=(BaseConf) t.newInstance();

			if(isConfStart(tempString)){
				clearConfList();
				return null;
			}
			if(isConfEnd(tempString)){
				return qe;
			}
			//System.out.println(tempString.substring(tempString.indexOf("=")+1));
			qe.setLineStart(line);
			qe.setLineEnd(line);
			Method method =Class.forName(t.getName()).getMethod("setValue",String.class);	
			method.invoke(qe,MyStringUtils.trimAll(tempString.substring(tempString.indexOf("=")+1)));
			addToConfList(qe);
			
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	protected boolean isConfEnd(String line) {
		if(line.contains(endTitle))
			return true;
		return false;
	}
	
	public boolean removeConf(String hostName){
		if(MyStringUtils.isEmpty(hostName))
			return false;
		removeConf(findConfByName(hostName));
		return true;
	}
	@Override
	public boolean removeConf(BaseConf bConf){
		if(bConf==null)return false;
		if(super.removeConf(bConf)){
			selectAllConf(new File(fileName), HostEntity.class);//修改完成后更改
			return true;
		}		
		return false;
		//System.out.println(getLastModified());
	}
	
	public boolean insertConf(String hostinfo){
		return insertConf(new HostEntity(hostinfo));
	}
	
	public boolean insertConf(BaseConf bConf){
		if(isEntityExist(bConf))return false;
		if(confList==null ||confList.isEmpty())return false;
		BaseConf b=(BaseConf)confList.get(confList.size()-1);//.getStart
		bConf.setLineStart(b.getLineStart());
		bConf.setLineEnd(bConf.getLineStart());
		super.updateFile(bConf, fileName, 1);
		super.selectAllConf(new File(fileName),HostEntity.class );//selectAllConf();
		//System.out.println(getLastModified());
		return true;
	}
	
	public List<BaseConf> selectAllConf(){
		return super.selectAllConf(fileName, HostEntity.class);
	}
	public static void main(String[] args){
		HostsConf queueConf=new HostsConf();
		List<BaseConf> list=queueConf.selectAllConf();
		HostEntity qe=new HostEntity("host2018    1   2    3    4    5    6    7");
		//queueConf.insertConf(qe);
		//queueConf.removeConf("host2");
		System.out.println(list.size());
		list=queueConf.selectAllConf();
		for(int index=0;index<list.size();index++){
			System.out.println(list.get(index));
		}
	}

}
